package com.yinhai.msg.email.service.write.impl;

import com.yinhai.msg.email.entity.MsgEmailPo;
import com.yinhai.msg.email.mapper.write.MsgEmailWriteMapper;
import com.yinhai.msg.email.service.write.MsgEmailWriteService;
import com.yinhai.ta404.core.exception.AppException;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.simplejavamail.MailException;
import org.simplejavamail.mailer.Mailer;
import org.simplejavamail.mailer.config.TransportStrategy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.Message;
import java.util.Properties;
import org.simplejavamail.email.Email;


/**
 * 邮箱配置表(MsgEmail)表服务实现类
 *
 * @author makejava
 * @since 2019-03-14 10:25:33
 */
@Service("msgEmailWriteService")
@TaTransactional
public class MsgEmailWriteServiceImpl implements MsgEmailWriteService {
    @Resource
    private MsgEmailWriteMapper msgEmailWriteMapper;

    /**
     * 新增数据
     *
     * @param msgEmail 实例对象
     * @return 实例对象
     */
    @Override
    public MsgEmailPo insert(MsgEmailPo msgEmail) throws MailException {
        //validateEmailAddress(msgEmail);
        this.msgEmailWriteMapper.insert(msgEmail);
        //当新增的邮箱是默认邮箱时，其他所有邮箱该为非默认
        if(msgEmail.getDefaultemail().equals("1")){
            this.msgEmailWriteMapper.updateDefaultEmail(msgEmail.getEmailaddress());
        }
        return msgEmail;
    }

    /**
     * 修改数据
     *
     * @param msgEmail 实例对象
     * @return 实例对象
     */
    @Override
    public MsgEmailPo update(MsgEmailPo msgEmail) throws MailException{
        //validateEmailAddress(msgEmail);
        //当修改成默认邮箱时，其他所有邮箱该为非默认
        if(msgEmail.getDefaultemail().equals("1")){
            this.msgEmailWriteMapper.updateDefaultEmail(msgEmail.getEmailaddress());
        }
        this.msgEmailWriteMapper.update(msgEmail);
        return msgEmail;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.msgEmailWriteMapper.deleteById(id) > 0;
    }

    /**
     * 验证邮箱是否真实有效
     * @param msgEmailPo
     */
    public Integer validateEmailAddress(MsgEmailPo msgEmailPo) throws MailException{
        //验证邮箱是否有效
        Email email = new Email();
        email.setFromAddress("",msgEmailPo.getEmailaddress());
        email.setSubject("测试连接!");
        email.addRecipient("",msgEmailPo.getEmailaddress(), Message.RecipientType.TO);
        email.setText("测试");

        Mailer mailer = null;
        if(msgEmailPo.getSendssl().equals("1")){
            mailer = new Mailer(msgEmailPo.getSendhost(),msgEmailPo.getSendport(),msgEmailPo.getEmailaddress(),msgEmailPo.getPassword(), TransportStrategy.SMTP_SSL);
        }else if(msgEmailPo.getSendtls().equals("1")){
            mailer = new Mailer(msgEmailPo.getSendhost(),msgEmailPo.getSendport(),msgEmailPo.getEmailaddress(),msgEmailPo.getPassword(),TransportStrategy.SMTP_TLS);
        }
        Properties props = new Properties();
        props.setProperty("mail.smtp.connectiontimeout", "10000");
        mailer.applyProperties(props);
        mailer.sendMail(email);
        return 1;
    }
}
